package logic

import (
	"context"
	"monitor/user/api/internal/svc"
	"monitor/user/api/internal/types"
	"monitor/user/model"
	//"fmt"

	"github.com/tal-tech/go-zero/core/logx"
)

type TermStatisticsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewTermStatisticsLogic(ctx context.Context, svcCtx *svc.ServiceContext) TermStatisticsLogic {
	return TermStatisticsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *TermStatisticsLogic) TermStatistics(token string) (*types.StatisticsReply, error) {
	// todo: add your logic here and delete this line
	claims := ParseToken(token)
	count, err := l.svcCtx.AlarmModel.FindAlarmStatistics(claims["city"].(string))
	stat, err := l.svcCtx.StatusModel.FindStatistics(claims["city"].(string))
	//fmt.Printf("count1-3:%v,%v,%v\n",count1,count2,count3)
	switch err {
	case nil:
		return &types.StatisticsReply{
			DevCount: stat.Count1,
			CurCount: stat.Count2,
			Online:   stat.Count3,
			UnRec:    stat.Unum1 + stat.Unum2,
			Alarmc:   count,
		}, nil
	case model.ErrNotFound:
		return nil, errorNotFound
	default:
		return nil, err
	}
}
